package org.example.controller;

import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.domain.R;
import org.example.domain.Setmeal;
import org.example.dto.SetmealDto;
import org.example.service.SetmealDishService;
import org.example.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {
    @Autowired
    SetmealService setmealService;
    @Autowired
    SetmealDishService setmealDishService;

    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithSetmealDish(setmealDto);
        return R.success("success");
    }

    @GetMapping("/page")
    public R<Page<SetmealDto>> page(int page, int pageSize, String name) {
        R<Page<SetmealDto>> withSetmealDishName = setmealService.getWithSetmealDishName(page, pageSize, name);
        return withSetmealDishName;
    }

    //    public R<Page<SetmealDto>> page(int page,int pageSize,String name){
//        Page<SetmealDto> setmealPage = new Page<>(page, pageSize);
//        LambdaQueryWrapper<SetmealDto> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        if (name!=null){
//            setmealLambdaQueryWrapper.like(SetmealDto::getName,name);
//            Page<SetmealDto> page1 = setmealService.page(setmealPage, setmealLambdaQueryWrapper);
//            return R.success(page1);
//        }
//
//        return R.success(setmealService.page(setmealPage));
//    }
    @DeleteMapping
    public R<String> delWithDish(@RequestParam List<Long> ids) {
        R<String> stringR = setmealService.delWithDish(ids);
        return stringR;
    }

    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        if (setmeal.getCategoryId() != null) {
            LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
            setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
            List<Setmeal> list = setmealService.list(setmealLambdaQueryWrapper);
            return R.success(list);
        }
        return R.success(setmealService.list());
    }

    @PostMapping("status/{status}")
    public R<String> setStatus(@PathVariable int status, Long[] ids) {
        for (Long id :
                ids) {
            Setmeal setmeal = setmealService.getById(id);
            setmeal.setStatus(status);
            setmealService.updateById(setmeal);
        }
        return R.success("ok");
    }
}
